home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 26 / develop issue 26 code / truffles - display mgr. / sprocket / sources / appleeventhandling.cp < prev    next >
Encoding:
Text File  |  1995-12-10  |  10.1 KB  |  358 lines

  1. /*
  2.     File:        AppleEventHandling.cp
  3.  
  4.     Contains:    Minimalist support for the required and Display Manager AppleEvents
  5.                 We also support openning and printing “LetterSpec” documents for AOCE.
  6.                 
  7.                 To really support AppleScript™, we’ll need to do ALOT more work in here.
  8.  
  9.     Written by: Dave Falkenburg
  10.  
  11.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  12.  
  13.     Change History (most recent first):
  14.      
  15.          <7>    12/15/94    DRF        Added ChooseApplicationAsAETarget.
  16.          <6>    11/23/94    DRF        InstallAppleEventHandlers is now called InitAppleEventRoutines.
  17.                                     Also added a useful global: gThisProcessDesc for self targetting
  18.                                     of AppleEvents.
  19.          <5>    11/16/94    DRF        Add StandardAEIdleProc for people who like using AESend with
  20.                                     kAEWaitReply.
  21.          <4>    11/12/94    DRF        Removed extra #include.
  22.          <3>     9/27/94    DRF         AppLib.h is now Sprocket.h
  23.          <2>      9/4/94    DRF        Added stub Text Services AppleEvent handlers.
  24.  */
  25.  
  26. #include "Sprocket.h"
  27.  
  28. #include <AppleEvents.h>
  29. #include <Errors.h>
  30. #include <Displays.h>            //    for Display Manager AppleEvent constants
  31. #include <OCEStandardMail.h>    //    for LetterSpec
  32.  
  33. #if    qInlineInputAware
  34. #include <TextServices.h>
  35. #endif
  36.  
  37. #include "AppleEventHandling.h"
  38.  
  39. static pascal Boolean StandardAEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn);
  40.     
  41.  
  42. AEDesc        gThisProcessDesc;
  43.  
  44. void
  45. InitAppleEventRoutines(void)
  46.     {
  47.     ProcessSerialNumber    thisProcess;
  48.  
  49.     //    Create useful AE Targets for our aplication
  50.  
  51.     //    By building the target in this fashion, we get the best performance
  52.     //    out of the AppleEvent manager because it can call our event handlers
  53.     //    inline.
  54.  
  55.     thisProcess.highLongOfPSN     = 0;
  56.     thisProcess.lowLongOfPSN     = kCurrentProcess;
  57.     (void) AECreateDesc(typeProcessSerialNumber,(Ptr)&thisProcess, sizeof(ProcessSerialNumber),&gThisProcessDesc);
  58.  
  59.  
  60.     //    It’s probably more efficient to use a table to install these handlers…
  61.     
  62.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,NewAEEventHandlerProc(HandleOpenApplication),0,false);
  63.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,NewAEEventHandlerProc(HandleOpenDocuments),0,false);
  64.     (void) AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,NewAEEventHandlerProc(HandlePrintDocuments),0,false);
  65.     (void) AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,NewAEEventHandlerProc(HandleQuitApplication),0,false);
  66.  
  67.     //    regardless of whether or not we have the display manager, go ahead and register an AppleEvent handler
  68.     (void) AEInstallEventHandler(kCoreEventClass,kAESystemConfigNotice,NewAEEventHandlerProc(HandleSystemConfigNotice),0,false);
  69.  
  70. #if    qInlineInputAware
  71.     if (gHasTextServices)
  72.         {
  73.         (void) AEInstallEventHandler(kTextServiceClass,kUpdateActiveInputArea,NewAEEventHandlerProc(HandleTextServicesUpdateActiveInputArea),0,false);
  74.         (void) AEInstallEventHandler(kTextServiceClass,kPos2Offset,NewAEEventHandlerProc(HandleTextServicesPos2Offset),0,false);
  75.         (void) AEInstallEventHandler(kTextServiceClass,kOffset2Pos,NewAEEventHandlerProc(HandleTextServicesOffset2Pos),0,false);
  76.         //    we don’t need to handle <kTextServiceClass,kShowHideInputWindow> events
  77.         }
  78. #endif
  79.     }
  80.  
  81.  
  82. OSErr
  83. CheckAppleEventForMissingParams(AppleEvent *theAppleEvent)
  84.     {
  85.     DescType    returnedType;
  86.     Size        actualSize;
  87.     OSErr        err;
  88.     
  89.     err = AEGetAttributePtr(theAppleEvent,keyMissedKeywordAttr,typeWildCard,
  90.                             &returnedType,nil,0,&actualSize);
  91.     
  92.     if (err == errAEDescNotFound)        //    If we couldn’t find the error attribute
  93.         return noErr;                    //        everything is ok, return noErr
  94.     
  95.     if (err == noErr)                    //    We found an error attribute, so
  96.         return errAEEventNotHandled;    //        tell the client we ignored the event
  97.  
  98.     return err;                            //    Something else happened, return it
  99.     }
  100.  
  101.  
  102. OSErr
  103. ChooseApplicationAsAETarget(AEAddressDesc *targetDesc, StringPtr prompt)
  104.     {
  105.     PortInfoRec        thePortInfo;
  106.     LocationNameRec    theLocation;
  107.     TargetID        theTargetID;
  108.     OSErr            err;
  109.  
  110.     err = PPCBrowser(prompt, "\p", false, &theLocation, &thePortInfo, (PPCFilterUPP) NULL, "\p");
  111.     if (err == noErr)
  112.         {
  113.         theTargetID.name = thePortInfo.name;
  114.         theTargetID.location = theLocation;
  115.  
  116.         err = AECreateDesc(typeTargetID,&theTargetID,sizeof(theTargetID),targetDesc);
  117.         }
  118.  
  119.     return err;
  120.     }
  121.  
  122.  
  123. OSErr
  124. SendOpenDocumentToSelf(LetterDescriptor * /* theDocument */)
  125.     {
  126.     return noErr;
  127.     }
  128.  
  129.  
  130. OSErr
  131. SendQuitApplicationToSelf()
  132.     {
  133.     AppleEvent    quitAE,replyAE;
  134.     OSErr        err;
  135.     
  136.     err = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &gThisProcessDesc, kAutoGenerateReturnID, kAnyTransactionID, &quitAE);
  137.     if (err != noErr)
  138.         {
  139.         err = AESend(&quitAE,&replyAE, kAENoReply + kAECanInteract, kAENormalPriority, 3600, NULL, NULL);
  140.         (void) AEDisposeDesc(&quitAE);
  141.         }
  142.     
  143.     return noErr;
  144.     }
  145.  
  146.  
  147.  
  148.  
  149. AEIdleUPP        StandardAEIdleUPP = NewAEIdleProc(StandardAEIdleProc);
  150.  
  151. static pascal Boolean
  152. StandardAEIdleProc(EventRecord *theEvent, long * /* sleepTime */, RgnHandle * /* mouseRgn */)
  153.     {
  154.     HandleEvent(theEvent);            //    First, always hand event off to our event handling code
  155.     return false;                    
  156.     }
  157.  
  158.  
  159. OSErr
  160. ForEachDocumentInList(AEDescList documentList,EachDocumentProcPtr documentProc,void * documentParam)
  161.     {
  162.     long                documentCount,documentIndex;
  163.     DescType            returnedType;
  164.     Size                actualSize;
  165.     LetterDescriptor    theLetterDesc;
  166.     AEKeyword            keyword;
  167.     OSErr                err;
  168.  
  169.     if ((err = AECountItems(&documentList,&documentCount)) != noErr)
  170.         return err;
  171.     
  172.     for (documentIndex=1; documentIndex <= documentCount; documentIndex++)
  173.         {
  174.         //    What kind of document is it?
  175.         if ((err = AESizeOfNthItem(&documentList,documentIndex,&returnedType,&actualSize)) != noErr)
  176.             return err;
  177.         
  178.         //    Is it an AOCE letter?
  179.         if (returnedType == typeLetterSpec)
  180.             {
  181.             //    It’s a letter
  182.             theLetterDesc.onDisk = false;
  183.             err = AEGetNthPtr(&documentList,documentIndex,typeLetterSpec,&keyword,&returnedType,
  184.                                 (Ptr) &theLetterDesc.u.mailboxSpec, sizeof(theLetterDesc.u.mailboxSpec),&actualSize);
  185.             }
  186.         else
  187.             {
  188.             //    It’s just a normal document file
  189.             theLetterDesc.onDisk = true;
  190.             err = AEGetNthPtr(&documentList,documentIndex,typeFSS,&keyword,&returnedType,
  191.                                 (Ptr) &theLetterDesc.u.fileSpec,sizeof(theLetterDesc.u.fileSpec),&actualSize);
  192.             }
  193.             
  194.         if (err == noErr)
  195.             (*documentProc)(&theLetterDesc,documentParam);
  196.         else
  197.             break;
  198.         }
  199.     
  200.     return    err;
  201.     }
  202.  
  203.     
  204. pascal OSErr
  205. HandleOpenApplication(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  206.     {
  207.     OSErr    err;
  208.     
  209.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  210.         return err;
  211.  
  212.     return(CreateNewDocument());
  213.     }
  214.  
  215.  
  216. pascal OSErr
  217. HandleOpenDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  218.     {
  219.     AEDescList            documentList;
  220.     OSErr                err;
  221.     
  222.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  223.         return err;
  224.  
  225.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  226.         return err;
  227.  
  228.     err = ForEachDocumentInList(documentList,&OpenDocument,nil);
  229.     (void) AEDisposeDesc(&documentList);
  230.     return err;
  231.     }
  232.  
  233.  
  234. pascal OSErr
  235. HandlePrintDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  236.     {
  237.     AEDescList            documentList;
  238. #if    qUseQuickDrawGX
  239.     AEDescList            desktopPrinterList;
  240.     FSSpec                thePrinterFSSpec;
  241.     Boolean                draggedToDesktopPrinter = false;
  242. #endif
  243.     OSErr                err;
  244.     
  245.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  246.         return err;
  247.  
  248. #if    qUseQuickDrawGX
  249.     if (noErr == AEGetAttributeDesc(theAppleEvent,keyOptionalKeywordAttr,typeAEList,&desktopPrinterList))
  250.         draggedToDesktopPrinter = true;
  251. #endif
  252.  
  253.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  254.         return err;
  255.  
  256. #if    qUseQuickDrawGX
  257.     if (draggedToDesktopPrinter)
  258.         {
  259.         DescType            returnedType;
  260.         Size                actualSize;
  261.         AEKeyword            keyword;
  262.  
  263.         err = AEGetNthPtr(&desktopPrinterList,1,typeFSS,&keyword,&returnedType,
  264.                             (Ptr) &thePrinterFSSpec,sizeof(thePrinterFSSpec),&actualSize);
  265.  
  266.         (void) AEDisposeDesc(&desktopPrinterList);
  267.         }
  268.     err = ForEachDocumentInList(documentList,&PrintDocument,draggedToDesktopPrinter ? &thePrinterFSSpec : NULL);
  269.     (void) AEDisposeDesc(&documentList);
  270. #else
  271.     err = ForEachDocumentInList(documentList,&PrintDocument,nil);
  272. #endif
  273.  
  274.     return err;
  275.     }
  276.  
  277.  
  278. pascal OSErr
  279. HandleQuitApplication(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  280.     {
  281.     OSErr    err;
  282.     
  283.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  284.         return err;
  285.  
  286.     gDone = QuitApplication();
  287.     
  288.     if (gDone)
  289.         return noErr;
  290.     else
  291.         return userCanceledErr;
  292.     }
  293.  
  294.  
  295. ////////////////////////////////////////////////////////////////////////
  296. //
  297. //    Display Manager Suite is “Under Construction”
  298. //
  299. //    To Do: Check ERS for Display Manager events
  300.  
  301. pascal OSErr
  302. HandleSystemConfigNotice(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  303.     {
  304.     AEDescList    displayList;
  305.     long        displayCount,displayIndex;
  306.     OSErr        err;
  307.     
  308.     DebugStr((StringPtr) "\pGot a Display Manager Event!");
  309.     
  310.     if ((err = AEGetParamDesc(theAppleEvent,kAEDisplayNotice,typeAEList,&displayList)) != noErr)
  311.         return err;
  312.     
  313.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  314.         return err;
  315.     
  316.     if ((err = AECountItems(&displayList,&displayCount)) != noErr)
  317.         return err;
  318.  
  319.     for (displayIndex = 1; displayIndex<=displayCount; displayIndex++)
  320.         {
  321.         DebugStr((StringPtr) "\pProcessing a display");
  322.         
  323.         //    Gather up all the old and new display rectangles into an oldDeskRegion and a newDeskRegion
  324.         }
  325.  
  326.     //    run through all windows calling keeping all windows which were onscreen in the oldDeskRegion
  327.     //    onscreen in the new world. We should really be calling a method to do this so that windows
  328.     //    can individually deal with things in a manner which they can override.
  329.  
  330.  
  331.     return errAEEventNotHandled;    //    we really don’t handle this yet...
  332.     }
  333.  
  334. #if    qInlineInputAware
  335.  
  336. pascal OSErr
  337. HandleTextServicesUpdateActiveInputArea(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  338.     {
  339.     DebugStr("\pTextServicesUpdateActiveInputArea");
  340.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  341.     }
  342.  
  343. pascal OSErr
  344. HandleTextServicesPos2Offset(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  345.     {
  346.     DebugStr("\pTextServicesPos2Offset");
  347.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  348.     }
  349.     
  350. pascal OSErr
  351. HandleTextServicesOffset2Pos(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  352.     {
  353.     DebugStr("\pTextServicesOffset2Pos");
  354.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  355.     }
  356.  
  357. #endif
  358.